<?php

// 员工管理类

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Crypt;

class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
	// 员工列表
    public function index(Request $request)
    {
		// 权限
		$admin_info = $this->getAdminName( Session('admin_id') );
		// 1是 所有权限
		if( $admin_info->type != 1 )
		{
			return back()->with( ['authInfo'=>'您没有此项权限' ] );
		}
		
		// 获取搜索内容
		$keyword = $request->input('keyword',"");
		if( empty($keyword) ){
			// 为空 说明没有搜索
			$one = 'a.id';
			$two = '!=';
			$three = '0';
		}else{
			// 判断是不是 11位数字
			$ptn='/^\d{11}$/';
			preg_match_all($ptn,$keyword,$arr);	
			if( count($arr,true)>1 )
			{
				// 是电话
				$one = 'a.phone';
				$two = '=';
				$three = $keyword;
			}else
			{
				// 是员工名
				$one = 'a.name';
				$two = 'like';
				$three = '%'.$keyword.'%';
			}
			
		}
		// 查询所有员工 每页10条数据
		$admin = DB::table('admin as a')
					->select( 'a.id','a.name','d.name as d_name','a.phone','a.sex','a.status','a.lasttime',DB::raw("case when a.status=1 then '正常' when a.status=2 then '已离职' end as adminStatus"),DB::raw("case when a.sex=1 then '男' when a.sex=2 then '女' end as adminSex"),DB::raw("case when a.type=1 then '超级' when a.type=2 then '普通' end as adminType") )
					->leftJoin('department as d','d.id','=','a.department_id')
					->where( $one,$two,$three ) 
					// 状态正常
					// ->where( 'a.status','=','1' ) 
					->orderBy( 'a.id','desc' ) 
					->paginate(20);
		return view( 'Admin.admin.list',[ 'admin'=>$admin,'request'=>$keyword ] );
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */

    // 员工 创建 编辑 页面
    public function create($id='')
    {
		// 权限
		$admin_info = $this->getAdminName( Session('admin_id') );
		// 1是 所有权限
		if( $admin_info->type != 1 )
		{
			return back()->with( ['authInfo'=>'您没有此项权限' ] );
		}
		
		// 获取所有部门
		$all_department = DB::table('department')->get();
		if( $id=='' )
		{
			// 创建
			return view('Admin.admin.add', ['title' => '员工添加','all_department'=>$all_department]);
		}else
		{
			// 编辑
			$admin_info = DB::table('admin')->where('id', $id)->first();
			return view('Admin.admin.add', ['title' => '员工修改', 'admin_info'=>$admin_info, 'all_department'=>$all_department]);
		}
		
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */



    // 执行 员工注册 编辑
    public function store( $admin_id='',Request $request )
    {
		$data = $request -> except('_token');
		if( empty($data['type']) ){
			$this->show_msg( '请选择权限' );
		}
		// 名字 验证 
		$name_length=mb_strlen( $data['name'],'utf-8' );
		if( $name_length<1 ){
			$this->show_msg('员工名不能为空');
		}
		if( $name_length>10 ){
			$this->show_msg('员工名字数被限制在10个字符内');
		}
		if( empty($data['department_id']) ){
			$this->show_msg( '请选择部门' );
		}
		if( empty($data['sex']) ){
			$this->show_msg( '请选择性别' );
		}
		if( empty($data['password']) ){
			$this->show_msg( '请输入密码' );
		}
		if( empty($data['repassword']) ){
			$this->show_msg( '请输入确认密码' );
		}
		if( $data['password']!=$data['repassword'] ){
			$this->show_msg( '两次密码输入不一致' );
		}
		$pwd_pattern = "/\w+/";
		preg_match_all($pwd_pattern,$data['password'],$pwd_arr);
		if( count($pwd_arr[0])<1 ){
			$this->show_msg( '请输入字母数字下划线' );
		}
		if( empty($data['phone']) ){
			$this->show_msg( '请输入手机号' );
		}
		
		unset($data['repassword']);
		
		if( $admin_id == '' )
		{
			$data['regtime']=date('Y-m-d H:i:s',time());
			$data['lasttime']=date('Y-m-d H:i:s',time());
			// 创建
			$insert_id = DB::table('admin')->insertGetId($data);
			if( $insert_id )
			{
				/*
				* 	写日志 
				* 	参数：$type		类型 1是前台用户 2是商品 3是订单 4是员工管理
				*		  $link_id	关联表的ID
				*		  $content	操作说明
				*/
				$this -> write_log(4,$insert_id,'新入员工 ['.$insert_id."] ".$data['name']);
				$this->show_msg( '员工创建成功' );
			}else
			{
				$this->show_msg( '操作失败' );
			}
		}else
		{
			// 编辑
			$update = DB::table('admin')->where('id',$admin_id)->update( $data );
			if( $update )
			{
				$this -> write_log(4,$admin_id,'编辑员工 ['.$admin_id."] ".$data['name']);
				$this -> show_msg( '员工编辑成功' );
			}else
			{
				$this -> show_msg( '操作失败' );
			}
		}
		

    }   
	

	// 执行 修改员工状态
	public function upStatus($admin_id,Request $request)
	{
		$data = $request -> except('_token');
		
		if( $admin_id == Session('admin_id') )
		{
			$this -> show_msg(' 无法对自己进行操作 ');
		}
		
        $res = DB::table('admin')
					// 用户ID
					-> where('id',$admin_id)
					-> update($data);
		if($res)
		{
			/*
			* 	写日志 
			* 	参数：$type		类型 1是前台用户 2是商品 3是订单 4是员工管理
			*		  $link_id	关联表的ID
			*		  $content	操作说明
			*/
			$admin_info = $this -> getAdminName($admin_id);
			$this -> write_log(4,$admin_id,'修改员工 ['.$admin_id.']'.$admin_info->name.' 为 已离职');
			// 修改成功
			$this -> show_msg('操作成功');
		}else
		{
			$this -> show_msg('操作失败');
		}
	}

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */


    public function show($id)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit()
    {
    	
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


	

}
